package com.sg.service.base.finance.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.finance.req.BatchQueryAccountCapitalSourceReq;
import com.sg.dto.base.finance.req.QueryAccountCapitalSourceDetailReq;
import com.sg.dto.base.finance.req.QueryAccountCapitalSourceListReq;
import com.sg.entity.OmsAccountCapitalSource;
import com.sg.service.base.finance.MOmsAccountCapitalSourceService;
import com.wicket.okrfinance.common.dto.FieldInfo;
import com.wicket.okrfinance.common.exception.BizException;
import com.wicket.okrfinance.dal.mapper.mbg.OmsAccountCapitalSourceMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsAccountCapitalSourceServiceImpl
        extends ServiceImpl<OmsAccountCapitalSourceMapper, OmsAccountCapitalSource>
        implements MOmsAccountCapitalSourceService {

    @Autowired
    OmsAccountCapitalSourceMapper omsAccountCapitalSourceMapper;

    /**
     * 7-1-05批量查询财务资金来源[7667]
     * gen by moon at 11/4/2023, 11:53:58 PM
     */
    @Trace(operationName = "7-1-05批量查询财务资金来源")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAccountCapitalSource> batchQueryAccountCapitalSource(BatchQueryAccountCapitalSourceReq reqDto) {

        try {
            QueryWrapper<OmsAccountCapitalSource> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getAccountCapitalSourceList() == null || reqDto.getAccountCapitalSourceList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("capital_source_id", reqDto.getAccountCapitalSourceList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAccountCapitalSource.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAccountCapitalSource.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAccountCapitalSource.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsAccountCapitalSource>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 7-1-05查询财务资金来源列表[7671]
     * gen by moon at 11/4/2023, 11:54:00 PM
     */
    @Trace(operationName = "7-1-05查询财务资金来源列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAccountCapitalSource> queryAccountCapitalSourceList(QueryAccountCapitalSourceListReq reqDto) {

        try {
            QueryWrapper<OmsAccountCapitalSource> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAccountCapitalSource.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAccountCapitalSource.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAccountCapitalSource.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("create_time");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsAccountCapitalSource>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 7-1-05查询财务资金来源详情[7708]
     * gen by moon at 11/7/2023, 1:13:08 PM
     */
    @Trace(operationName = "7-1-05查询财务资金来源详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsAccountCapitalSource queryAccountCapitalSourceDetail(QueryAccountCapitalSourceDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsAccountCapitalSource> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsAccountCapitalSource> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsAccountCapitalSource retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 7-1-05新增财务资金来源[8046]
     * gen by moon at 2/24/2024, 12:29:57 AM
     */
    @Trace(operationName = "7-1-05新增财务资金来源")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addAccountCapitalSource(OmsAccountCapitalSource omsAccountCapitalSource) {
        omsAccountCapitalSource.setCapitalSourceId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsAccountCapitalSource.setCapitalSourceCode(CommonFunctionHelper.getUid());//AUTO_GENERATE
        omsAccountCapitalSource.setIsArchive("FALSE");
        omsAccountCapitalSource.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsAccountCapitalSource.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAccountCapitalSource != null && omsAccountCapitalSource.getSubjectLifeCycle() != null && omsAccountCapitalSource.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAccountCapitalSource.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsAccountCapitalSource.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsAccountCapitalSource.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAccountCapitalSource != null && omsAccountCapitalSource.getSubjectLifeCycle() != null && omsAccountCapitalSource.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAccountCapitalSource.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsAccountCapitalSource.setIsValid("FALSE");
        omsAccountCapitalSource.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsAccountCapitalSource.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsAccountCapitalSource)) {
            return omsAccountCapitalSource.getCapitalSourceId();
        } else {
            return "-1";
        }

    }
}
